﻿using MRGSP.SurveyMaster.Core.Model;
using MRGSP.SurveyMaster.Core.Service;
using MRGSP.SurveyMaster.Infra.Dto;
using MRGSP.SurveyMaster.Infra.ValueInjections;
using Omu.ValueInjecter;

namespace MRGSP.SurveyMaster.Infra
{
    public class UserEditBuilder : IBuilder<User, UserEditInput>
    {
        private readonly IUserService userService;

        public UserEditBuilder(IUserService userService)
        {
            this.userService = userService;
        }

        public UserEditInput BuildInput(User entity)
        {
            var input = new UserEditInput();
            input.InjectFrom<ByteToLookup>(entity)
                .InjectFrom(entity);
            return input;
        }

        public User BuildEntity(UserEditInput input)
        {
            var o = userService.Get(input.Id);
            o.InjectFrom<LookupToByte>(input);

            if (!string.IsNullOrEmpty(input.NewPassword))
                o.Password = input.NewPassword;

            return o;
        }

        public UserEditInput RebuildInput(UserEditInput input)
        {
            return BuildInput(BuildEntity(input));
        }
    }
}